﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using LRSTaxServices.DAL;
using LRSTaxServices.DAL.dsTaxNewsTableAdapters;
using LRSTaxServices.Common;

namespace LRSTaxServices.Biz
{
    /// <summary>
    /// TaxNews Business Object
    /// </summary>

    public class TaxNews : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected TaxNews() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the TaxNews.
        /// </summary>
        public int TaxNewsID
        {
            get { return _taxnewsid; }
            set
            {
                if (_taxnewsid != value)
                {
                    if (OnPropertyChanging("TaxNewsID", _taxnewsid, value))
                    {
                        int oldValue = _taxnewsid;
                        _taxnewsid = value;

                        OnPropertyChanged("TaxNewsID", oldValue, value);
                    }
                }
            }
        }  private int _taxnewsid = -1;

        /// <summary>
        /// TaxNewsTitle .
        /// </summary>
        public string TaxNewsTitle
        {
            get { return _taxnewstitle; }
            set
            {
                if (_taxnewstitle != value)
                {
                    if (OnPropertyChanging("TaxNewsTitle", _taxnewstitle, value))
                    {
                        string oldValue = _taxnewstitle;
                        _taxnewstitle = value;
                        OnPropertyChanged("TaxNewsTitle", oldValue, value);
                    }
                }
            }
        }  private string _taxnewstitle = null;



        /// <summary>
        /// TaxNewsDescription .
        /// </summary>
        public string TaxNewsDescription
        {
            get { return _taxnewsdescription; }
            set
            {
                if (_taxnewsdescription != value)
                {
                    if (OnPropertyChanging("TaxNewsDescription", _taxnewsdescription, value))
                    {
                        string oldValue = _taxnewsdescription;
                        _taxnewsdescription = value;
                        OnPropertyChanged("TaxNewsDescription", oldValue, value);
                    }
                }
            }
        }  private string _taxnewsdescription = null;



        /// <summary>
        /// Date/time at which the Schedule was added.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addedDate; }
            set
            {
                if (_addedDate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addedDate, value))
                    {
                        DateTime? oldValue = _addedDate;
                        _addedDate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addedDate = null;


        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobCategory object.
        /// </summary>
        public static TaxNews CreateNew()
        {
            TaxNews obj = new TaxNews();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            obj.AddedDate = DateTime.Now; ;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static TaxNews CreateNew(TaxNewsCollection collection)
        {
            TaxNews obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (TaxNews taxnews in collection)
            {
                if (taxnews.TaxNewsID < minVal)
                    minVal = taxnews.TaxNewsID;
            }
            obj.TaxNewsID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllTaxNews

        /// <summary>
        /// Returns a collection of all TaxNews.
        /// </summary>
        public static TaxNewsCollection GetAllTaxNews()
        {
            TaxNewsTableAdapter adapter = new TaxNewsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsTaxNews.TaxNewsDataTable tbl = adapter.GetData(null);

            TaxNewsCollection list = new TaxNewsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsTaxNews.TaxNewsRow row in tbl.Rows)
                {
                    TaxNews taxnews = new TaxNews();
                    RelationalToObject(taxnews, row);
                    list.Add(taxnews);
                }
            }

            return (list);
        }

        #endregion

        #region GetTaxNewsByID

        /// <summary>
        /// Returns a TaxNews object for the specified 'TaxNewsID' or null if no TaxNews with the specified parameter(s) exists.
        /// </summary>
        public static TaxNews GetTaxNewsByID(int taxnewsid)
        {
            TaxNewsTableAdapter adapter = new TaxNewsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsTaxNews.TaxNewsDataTable tbl = adapter.GetData(taxnewsid);

            if (tbl.Rows.Count == 1)
            {
                TaxNews TaxNews = new TaxNews();
                RelationalToObject(TaxNews, tbl.Rows[0]);
                return (TaxNews);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'TaxNews' returned more than one TaxNews record.");

            return (null);
        }

        #endregion

        #region GetTaxNewsByTaxNewsTitle

        /// <summary>
        /// Returns a TaxNews object for the specified 'DocumentName' or null if no TaxNews with the specified parameter(s) exists.
        /// </summary>
        public static TaxNews GetTaxNewsByTaxNewsTitle(string taxnewstitle)
        {
            TaxNewsTableAdapter adapter = new TaxNewsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsTaxNews.TaxNewsDataTable tbl = adapter.GetDataByTaxNewsTitle(taxnewstitle);

            if (tbl.Rows.Count == 1)
            {
                TaxNews taxnews = new TaxNews();
                RelationalToObject(taxnews, tbl.Rows[0]);
                return (taxnews);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'TaxNews' returned more than one TaxNews record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified TaxNews.
        /// </summary>
        public static void Update(TaxNews obj)
        {
            bool updateTaxNews = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateTaxNews = (obj.ObjState != ObjectState.Unchanged);

                if (!updateTaxNews)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsTaxNews.TaxNewsDataTable tbl = null;
            if (updateTaxNews)
            {
                TaxNewsTableAdapter adapter = new TaxNewsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsTaxNews.TaxNewsDataTable();
                dsTaxNews.TaxNewsRow row = tbl.NewTaxNewsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (TaxNewsID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("TaxNewsID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // TaxNewsTitle is required.
                if ((TaxNewsTitle == null) || (TaxNewsTitle.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("TaxNewsTitle", "TaxNewsTitle is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("TaxNews: TaxNewsID={0}, TaxNewsTitle={1}", TaxNewsID, TaxNewsTitle));
        }

        #endregion

        #endregion

    }

    /// <summary>
    /// Collection of 0 or more TaxNews objects.
    /// </summary>

    public class TaxNewsCollection : List<TaxNews>
    {
        #region Types

        public class UserComparer : IComparer<TaxNews>
        {
            public int Compare(TaxNews r1, TaxNews r2)
            {
                return (String.Compare(r1.TaxNewsTitle, r2.TaxNewsTitle));
            }
        }

        #endregion

        #region Public Methods

        #region FindByTaxNewsID

        public TaxNews FindByTaxNewsID(int taxnewsid)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].TaxNewsID == taxnewsid)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }
}
